﻿//
// LogOnViewModel.cs
//
// Authors:
//    Anders Gade Nørskov <09549@iha.dk
//    Lars Rønde Hansen <09548@iha.dk>
//    Claus Jørgensen <10229@iha.dk>
//
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Input;

using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;

namespace RSD.ViewModel
{
    using Data;
    using Messages;

    public class LogOnViewModel : ViewModelBase
    {
        /// <summary>
        /// UserName backing field.
        /// </summary>
        private string userName;
        
        /// <summary>
        /// Password backing field.
        /// </summary>
        private string password;
        
        /// <summary>
        /// LogOn command.
        /// </summary>
        private readonly RelayCommand logOnCommand;
        
        /// <summary>
        /// User repository.
        /// </summary>
        private readonly IUserRepository userRepository;

        /// <summary>
        /// Initializes a new instance of the <see cref="LogOnViewModel"/> class.
        /// </summary>
        /// <param name="userRepository">The user repository.</param>
        public LogOnViewModel(IUserRepository userRepository)
        {
            this.userRepository = userRepository;
            this.logOnCommand = new RelayCommand(LogOnExecute);
        }

        /// <summary>
        /// Gets or sets the name of the user.
        /// </summary>
        /// <value>The name of the user.</value>
        public string UserName
        {
            get
            {
                return userName;
            }
            set
            {
                userName = value;
                RaisePropertyChanged("UserName");
            }
        }

        /// <summary>
        /// Gets or sets the password.
        /// </summary>
        /// <value>The password.</value>
        public string Password
        {
            get
            {
                return password;
            }
            set
            {
                password = value;
                RaisePropertyChanged("Password");
            }
        }

        /// <summary>
        /// Gets the LogOn command.
        /// </summary>
        /// <value>The log on command.</value>
        public ICommand LogOnCommand
        {
            get
            {
                return logOnCommand;
            }
        }

        /// <summary>
        /// Executes the LogOn command.
        /// </summary>
        private void LogOnExecute()
        {
            var cancellationToken = new CancellationTokenSource();

            var task = Task.Factory.StartNew<User>(() => 
                userRepository.GetUserByNameAndPassword(UserName, Password));
            
            task.ContinueWith(t =>
            {
                if (t.IsFaulted || t.IsCanceled)
                {
                    Messenger.Default.Send(new ExceptionMessage(t.Exception));
                }
                else
                {
                    if (t.Result != null)
                    {
                        Messenger.Default.Send(new UserLoggedOnMessage(t.Result));
                    }
                    else
                    {
                        Messenger.Default.Send(new InvalidCredentialsMessage());
                    }
                }
            });
        }
    }
}
